<?php
/**
 * 
 * Enter description here ...
 * 
 * structure of options is as follows
 * 
 *  array(
 *  	'with_button'=> true,
 *  	//when true the error fields will have the designated error labels shown
 *  	'with_errors'=> true,
 *  	'error_fields'=> array(
 *  		'form input id' => array(
 *  			'input_id'=> 'form input id',
 *  			'error_label'=> 'custom error label'
 *  		)
 *  	),
 *  	'set_values'=> array(
 *  		'form input id' => array(
 *  			'input_id'=> 'form input id',
 *  			'value'=> 'custom error label'
 *  		)
 *  	)
 *  )
 * 
 */
class ClaycodeFormGenerator
{
	protected $arr_formConfig;
	protected $arr_options;
	
	public function __construct($arr_options, $arr_formConfig)
	{
		$this->arr_options = $arr_options;
		$this->arrFormConfig = $arr_formConfig;
	}
	
	public function getFormHtml()
	{
		$s_formHtml = '';
		if(SessionHandler::getInstance()->browsingSafe() == true){
			$s_formHtml = $this->makeFormElem($this->makeFormFields());
		}
		
		return $s_formHtml;
	}
	
	public function makePostSubmitHandler()
	{
		$s_formSubmitHandlerField = Claycode_configuration::getInstance()->getFormPostSubmitHandler();
		$s_formSubmitIdentifier = fw_util_encrypt(date('U') . $s_formSubmitHandlerField . $this->arrFormConfig['form_ident_alias']);
		
		$s_formPostSubmitHandler = '<input type="hidden" name="'
		. $s_formSubmitHandlerField
		. '" value="'
		. $s_formSubmitIdentifier
		. '">';
		
		$this->writeToSessionFormSubmitHandleId($s_formSubmitIdentifier);
		
		return $s_formPostSubmitHandler;
	}
	
	public function writeToSessionFormSubmitHandleId($s_handlerId)
	{
		$s_sessionBrowseKey = SessionHandler::getInstance()->getSessVarKey();
		$_SESSION[$s_handlerId] =  $s_sessionBrowseKey . $this->arrFormConfig['form_ident_alias'];
		
	}
	
	private function makeFormElem($s_formContents)
	{
		$s_formCont = '<form method="'
					. $this->arrFormConfig['method']
					. '" name="'
					. $this->arrFormConfig['form_name']
					. '" action="'
					. $this->arrFormConfig['action']
					. '">'
					. $s_formContents
					. $this->makeIdentifierField()
					. $this->makePostSubmitHandler()
					. '</form>';
		return $s_formCont;
	}
	
	private function getFormButton($arr_submitButtonConfig = null)
	{
		if($this->arr_options['with_button'] == true){
			if($arr_submitButtonConfig != null){
				//$s_formButton = '<input type="submit" value="" />';
				$s_formButton = '<button type="submit" id="'. $arr_submitButtonConfig['id'] .'">' . $arr_submitButtonConfig['value'] . '</button>';
			}else{
				$s_formButton = '<button type="submit" id="submit_button">submit</button>';
			}
		}
		
		return $s_formButton;
	}
	
	private function makeFormFields()
	{
		$s_formFieldsHtml = '';
		$arr_fieldSets = $this->arrFormConfig['form_fields'];
		foreach($arr_fieldSets as $arr_fieldSet){
			$s_formFieldsHtml .= '<fieldset id="' . $arr_fieldSet['field_set_id'] . '">';
			$s_formFieldsHtml .= '<legend>' . $arr_fieldSet['field_set_label'] . '</legend>';
			$arr_fields = $arr_fieldSet['fields'];
			//loop through the form configuration array fields
			$s_formFieldsHtml .= '<ul>';
			foreach($arr_fields as $arr_fieldConfig) {
				$s_formFieldsHtml .= $this->makeFormLine($arr_fieldConfig);
			}
			$s_formFieldsHtml .= '<li>' . $this->getFormButton($this->arrFormConfig['submit_button']) . '</li>';
			$s_formFieldsHtml .= '</ul>';
			$s_formFieldsHtml .= '</fieldset>';
		}
		
		return $s_formFieldsHtml;
	}
	
	private function makeFormLine($arr_fieldConfig)
	{
		/*
		$s_formLine  = '<li>';
		$s_formLine .= $this->makeFormFieldLabel($arr_fieldConfig);
		$s_formLine .= '</li>';
		
		$s_formLine .= '<li>';
		$s_formLine .= $this->makeFormField($arr_fieldConfig);
		$s_formLine .= '</li>';
	
		$s_formLine .= '<li>';
		$s_formLine .= $this->makeFormErrorLine($arr_fieldConfig);
		$s_formLine .= '</li>';
		return $s_formLine;
		*/
		if($arr_fieldConfig['input_type'] != 'hidden'){
			$s_formLine  = '<li>';
			$s_formLine .= $this->makeFormFieldLabel($arr_fieldConfig) . $this->makeFormField($arr_fieldConfig);
			$s_formLine .= '</li>';
			
			$s_formLine .= '<li>';
			$s_formLine .= $this->makeFormErrorLine($arr_fieldConfig);
			$s_formLine .= '</li>';
		}else{
			$s_formLine = $this->makeFormField($arr_fieldConfig);
		}
		return $s_formLine;
	}
	
	private function makeFormFieldLabel($arr_fieldConfig)
	{
		if($arr_fieldConfig['input_type'] != 'hidden'){
			//$s_formFieldLabel  = '<div class="form-field-label">';
			//$s_formFieldLabel .= $arr_fieldConfig['field_label'];
			//$s_formFieldLabel .= '</div>';
			$s_formFieldLabel  = '<label>';
			$s_formFieldLabel .= $arr_fieldConfig['field_label'];
			$s_formFieldLabel .= '</label>';
		}
		
		return $s_formFieldLabel;
	}
	
	private function makeFormErrorLine($arr_fieldConfig)
	{
		if($arr_fieldConfig['input_type'] != 'hidden'){
			$s_formErrorLine = '<div id="error_'
							 . $arr_fieldConfig['input_id']
							 . '" class="form-error"></div>';
			if($this->arr_options['with_errors'] == true){
				if(isset($this->arr_options['error_fields'][$arr_fieldConfig['input_id']])){
					$s_formErrorLine = '<div id="error_'
									 . $arr_fieldConfig['input_id']
									 . '" class="form-error">'
									 . $this->arr_options['error_fields'][$arr_fieldConfig['input_id']]['error_label']
									 . '</div>';
				}
			}
		}
		
		return $s_formErrorLine;
	}
	
	private function makeFormField($arr_fieldConfig)
	{
		$s_formField = '';
		switch ($arr_fieldConfig['input_type']){
			case 'password':
				$s_formField .= '<input '
							 . 'type="password" name="'
							 . $arr_fieldConfig['input_name']
							 . '" id="'
							 . $arr_fieldConfig['input_id']
							 . '" value="'
							 . $this->getFormFieldValue($arr_fieldConfig)
							 . '">';
				break;
				
			case 'textArea':
				$s_formField .= '<textarea name="'
							 . $arr_fieldConfig['input_name']
							 . '" id="'
							 . $arr_fieldConfig['input_id']
							 . '" >'
							 . $this->getFormFieldValue($arr_fieldConfig)
							 . '</textarea>';
				break;
	
			case 'text':
				$s_formField .= '<input '
							 . 'type="text" name="'
							 . $arr_fieldConfig['input_name']
							 . '" id="'
							 . $arr_fieldConfig['input_id']
							 . '" value="'
							 . $this->getFormFieldValue($arr_fieldConfig)
							 . '">';
				break;
	
			case 'radio':
	
				break;
				
			case 'hidden':
				$s_formField .= '<input '
							 . 'type="hidden" name="'
							 . $arr_fieldConfig['input_name']
						     . '" id="'
							 . $arr_fieldConfig['input_id']
							 . '" value="'
							 . $this->getFormFieldValue($arr_fieldConfig)
							 . '">';
				break;
	
			default:
				$s_formField .= 'error in configuration';
			break;
		}
	
		return $s_formField;
	}
	
	private function getFormFieldValue($arr_fieldConfig)
	{
		$s_fieldValue = '';
		if(isset($this->arr_options['set_values'][$arr_fieldConfig['input_id']])){
			$arr_fieldValues = $this->arr_options['set_values'];
			$s_fieldValue = $arr_fieldValues[$arr_fieldConfig['input_id']]['value'];
		}else if(!isset($this->arr_options['set_values'][$arr_fieldConfig['input_id']]) && $arr_fieldConfig['value'] != ''){
			$s_fieldValue = $arr_fieldConfig['value'];
		}
		return $s_fieldValue;
	}
	
	public function makeIdentifierField()
	{
		$s_formIdentField 	= '<input type="hidden" name="'
							. Claycode_configuration::getInstance()->getFormIdentField()
							. '" value="'
							. $this->arrFormConfig['form_ident_alias']
							. '">';
		return $s_formIdentField;
	}
	
	public function __destruct()
	{
	
	}
}
?>